export function host (url) {
  const host = url.replace(/^https?:\/\//, '').replace(/\/.*$/, '')
  const parts = host.split('.').slice(-3)
  if (parts[0] === 'www') parts.shift()
  return parts.join('.')
}

export function timeAgo (time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

// 运行事件换算
export function duration (val) {

  let message = (val / 86400000).toFixed(0) + ' d  ' + (val % 86400000 / 3600000).toFixed(0) + ' h  ' + (val % 86400000 % 3600000 / 60000).toFixed(0) + ' m  ' + (val % 86400000 % 3600000 % 60000 / 1000).toFixed(0) + ' s  ' + (val % 86400000 % 3600000 % 60000 % 1000) + ' ms'
  switch (val) {
    case val >= 86400000:
      message = (val / 86400000).toFixed(0) + ' d  ' + (val % 86400000 / 3600000).toFixed(0) + ' h  ' + (val % 86400000 % 3600000 / 60000).toFixed(0) + ' m  ' + (val % 86400000 % 3600000 % 60000 / 1000).toFixed(0) + ' s  ' + (val % 86400000 % 3600000 % 60000 % 1000) + ' ms'
      break
    case val >= 3600000:
      message = (val % 86400000 / 3600000).toFixed(0) + ' h  ' + (val % 86400000 % 3600000 / 60000).toFixed(0) + ' m  ' + (val % 86400000 % 3600000 % 60000 / 1000).toFixed(0) + ' s  ' + (val % 86400000 % 3600000 % 60000 % 1000) + ' ms'
      break
    case val >= 60000:
      message = (val % 86400000 % 3600000 / 60000).toFixed(0) + ' m  ' + (val % 86400000 % 3600000 % 60000 / 1000).toFixed(0) + ' s  ' + (val % 86400000 % 3600000 % 60000 % 1000) + ' ms'
      break

  }
  return message

}

export function timeChange (time) {

  let t = new Date()
  t.setTime(time)
  return t

}

export function timeTransformation (val) {
  return new Date(parseInt(val)).toLocaleString().replace(/:\d{1,2}$/, ' ')
}

function pluralize (time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

export function toSta (val) {
  switch (val) {
    case '0':
      return '未运行'
      break
    case '1':
      return '运行中'
      break
    case '2':
      return '已终止'
      break
    case '3':
      return '已完成'
      break
    case '4':
      return '提交中'
      break
    default:
      return val
      break
  }
}
export function todataauthsta (val) {
  switch (val) {
    case 0:
      return '正在创建'
      break
    case 1:
      return '创建成功'
      break
    case 2:
      return '创建失败'
      break
    default:
      return val
      break
  }
}

export function toReSta (val) {
  return (val == '1' || val == true) ? '重新启动' : '停止'
}

export function toReStop (val) {
  return (val == '1' || val == true) ? '停止' : '重新启动'
}

export function isEnabled (val) {
  return (val == '1' ) ? '可用' : '不可用'
}

export function databaseType (val) {
  return (val == '1' ) ? 'mysql' : 'oracle'
}

export function toYesNo (val) {
  return val == '1' ? '是' : '否'
}

export function taskType (valo) {
  let val = valo
  try {
    val = parseInt(val)
  }
  catch (e) {
    return valo
  }
  if (isNaN(val))return valo
  switch (val) {
    case 1:
      return 'spark'
      break
    case  2:
      return 'mapreduce'
      break
    case 3:
      return 'hive'
      break
    case 4:
      return 'sqoop'
      break
    case 5:
      return 'shell'
      break
    case 6:
      return '工作流'
      break
    case 7:
      return 'python'
      break
    default:
      return val
  }
}

//pange311 任务运行状态
export function taskRunState (val) {

  let taskRunState
  if (val == '1') {
    taskRunState = '运行中'
  } else if (val == '2') {
    taskRunState = '手动终止'
  } else if (val == '3') {
    taskRunState = '完成'
  } else if (val == '4') {
    taskRunState = '提交中'
  } else {
    taskRunState = '未运行'
  }
  return taskRunState

}

export function singleBtn (val) {

  let tips;
  (val == '1') ? tips = true : tips = false
  return tips

}
export function timerBtn (val) {

  let tips;
  (val == '1') ? tips = '启用' : tips = '禁用'
  return tips

}

//page71 任务类型
export function taskRunType (val) {
  let taskRunType
  if (val == '1') {
    taskRunType = '一次性任务'
  } else if (val == '2') {
    taskRunType = '定时任务'
  } else {
    taskRunType = '暂无数据'
  }
  return taskRunType
}

export function runResult (val) {

  if (val == '0') return '成功'
  else if (val == '1') return '失败'
  else val

}

export function  toSta1 (val) {

    if (val == '1') return '成功'
    else if (val == '2') return '失败'
    else val

}

export function taskResult (val) {

  let message = ''
  let n = val || 0;
  (n == '1') ? message = '成功' : message = '失败'
  return message

}

export function is_enabled (val) {

  return val == '0' ? '是' : '否'

}
export function is_missing (val) {

  return val == '0' ? '否' : '是'

}
export function toTime (val) {

  if (!val) return val
  return new Date(val)
}
export function toTimeStr (val) {
  let reg=/^[0-9]*$/;
  if(!reg.test(val)) return val
  if (!val) return val
  return new Date(val).toString()
}


/**
 * Page621 分区数量
 * @param number
 * @returns string
 */
export function partNum (val) {

  let num = val || 0
  let tips = '';
  (num == '-1') ? tips = '不是分区表' : tips = num
  return tips
}

/**
 * Page81 系统审计 操作行为
 * @param number
 * @returns string
 */
export function handleType (val) {

  let n = parseInt(val) || 0
  let typeName = ''
  switch (n) {
    case 1:
      typeName = '查询'
      break
    case 2:
      typeName = '增加'
      break
    case 3:
      typeName = '修改'
      break
    case 4:
      typeName = '删除'
      break
  }
  return typeName

}

/**
 * KB-MB单位转换
 * @param KB number
 * @returns MB number
 */
export function fileSize (val) {

  let n = val || 0
  n = n / 1024 / 1024
  return n

}

/**
 * KB单位转换
 * @param B number
 * @returns kB number
 */
export function fileSizeKB (val) {

  let n = val || 0
  n = (n / 1024).toFixed(2)
  return n

}
/**
 * 角色类型
 * @param number
 * @returns string
 */
export function roleType (val) {

  let n = val || 0
  let m = '';
  (n == 1) ? m = '管理员' : m = '普通'
  return m

}

export function getLocalTime(nS) {
  return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/,' ');
}
export function getTimex(startTime,endTime) {
  if(!startTime|| !endTime) return ''
  var date1=new Date(startTime);  //开始时间
  var date2=new Date(endTime);    //结束时间
  var date3=date2.getTime()-date1.getTime()  //时间差的毫秒数
//计算出相差天数
  var days=Math.floor(date3/(24*3600*1000))

//计算出小时数

  var leave1=date3%(24*3600*1000)    //计算天数后剩余的毫秒数
  var hours=Math.floor(leave1/(3600*1000))
//计算相差分钟数
  var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数
  var minutes=Math.floor(leave2/(60*1000))

//计算相差秒数
  var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
  var seconds=Math.round(leave3/1000)

  return days+"天 "+hours+"小时 "+minutes+" 分钟"+seconds+" 秒"
}


export function isMissing(val){
  return val=="1" ?  "是":"否";
}

export function filterSize(val) {
  return (parseInt(val) / 1024).toFixed(4)
}

export function toSec(val) {
  if(!val) return "";
  return `${val/1000} 秒`
}